// pages/crowd/crowdOrder/index.js
var common = require("../../../public.js");
Page({
  data: {
    orderInfo: {},
    userAddr: [],
    currentAddr: 0,
    showPop: false,
    selectedAddr: false, //地址
  },
  onLoad: function (options) {
    let that = this
    wx.getStorage({
      key: 'user',
      success: function (res) {
        if (options.params) {
          that.setData({
            userInfo: res.data.user,
            token: res.data.token,
            openid: res.data.openid,
            orderInfo: JSON.parse(options.params)
          }, () => {
            that.getUserAddress()
          })
        } else if (!options.params) {
          console.log("44555")
          that.setData({
            userInfo: res.data.user,
            token: res.data.token,
            openid: res.data.openid,
            selectedAddr: true,
            orderInfo: JSON.parse(options)
          }, () => {
            console.log(that.data.orderInfo)
            that.getUserAddress()
          })
        }
      },
    })
  },
  getUserAddress() {
    let that = this
    let tempJson = {
      userId: that.data.userInfo.id,
      token: that.data.token
    }
    common.post("/user/ship/list", tempJson, function (res) {
      if (res.status === 200) {
        let currentAddr = ''
        res.data.map((item, index) => {
          item.isDefault == 1
          currentAddr = index
        })
        that.setData({
          userAddr: res.data,
          currentAddr
        },
          () => {
            console.log(that.data.currentAddr, "是个人")
          }
        )
      } else {
        common.showRequestErrMsg(res)
      }
    })
  },
  // 添加地址
  go_address() {
    wx.navigateTo({
      url: '/packageA/pages/Goods/Address/list/index?from=crowdOrder' + '&orderInfo=' + JSON.stringify(this.data.orderInfo),
    })
  },
  payNow() {
    let that = this
    let tempJson = {
      crowdfundingId: this.data.orderInfo.crowdfundingId,
      setmealId: this.data.orderInfo.setmeal.id,
      payNum: this.data.orderInfo.payNum,
      shippingId: !that.data.selectedAddr ? this.data.userAddr[this.data.currentAddr].id : that.data.orderInfo.id,
      buyUserId: this.data.userInfo.id,
      userId: this.data.userInfo.id,
      token: this.data.token
    }

    common.post('/crowdfunding/apply', tempJson, function (res) {
      if (res.status == 200) {
        let payParam = {
          userId: that.data.userInfo.id,
          token: that.data.token,
          orderId: res.data.id,
          openid: that.data.openid
        }
        common.wxPayRequest("/wxxcxPayForCrowdfundingGoods", payParam, function (payRes) {
          if (payRes.msg == 'OK') {
            //微信支付
            wx.requestPayment({
              'timeStamp': payRes.data.timeStamp,
              'nonceStr': payRes.data.nonceStr,
              'package': payRes.data.package,
              'signType': payRes.data.signType,
              'paySign': payRes.data.paySign,
              'success': function (res) {
                common.showToast('支付成功', 'success')
                wx.navigateTo({
                  url: '/pages/crowd/OrderDetail/OrderDetail',
                })
              },
              'fail': function (res) {
                console.log(res)
                if (res.errMsg === 'requestPayment:fail cancel') {
                  wx.navigateTo({
                    url: '/pages/crowd/OrderDetail/OrderDetail',
                  })
                } else {
                  common.showToast(res.errMsg)
                  wx.navigateTo({
                    url: '/pages/crowd/OrderDetail/OrderDetail',
                  })
                }
              }
            })
          }
        })
      } else {
        common.showRequestErrMsg(res)
      }
    })
  },
  clickAddr() {
    wx.navigateTo({
      url: '/packageA/pages/Goods/Address/list/index?from=crowdOrder',
    })
    // this.setData({
    //   showPop: true
    // })
  },
  onClose() {
    this.setData({
      showPop: false
    })
  },
  selectAddr(e) {
    let { index } = e.currentTarget.dataset
    this.setData({
      currentAddr: index
    }, _ => {
      this.onClose()
    })
  },
})